// eee.Sheffield.PZ.Imaging
//
// Copyright ?Ping Zou, 2007
// sg71.cherub@gmail.com

using System;
using System.Collections.Generic;
using System.Text;
using eee.Sheffield.PZ.Math;
using System.Drawing;
using System.Drawing.Imaging;

namespace eee.Sheffield.PZ.Imaging.ImageFilter
{
    /// <summary>
    /// two image add
    /// </summary>
    public class TwoImageAdd : ImageFilter//, IGraylevelImageFilter
    {
        #region Fields
        private Bitmap _imageToMinus;
        #endregion

        #region Properties
        #endregion

        #region Constructor
        /// <summary>
        /// srcImage - imageToMinus
        /// </summary>
        /// <param name="imageToMinus"></param>
        public TwoImageAdd(Bitmap imageToMinus)
        {
            _imageToMinus = imageToMinus;
            _flag = "[Image -> Two Image -> Add]";
        }
        #endregion

        #region Apply filter
        protected override PZMath_matrix ApplyDoubleGraylevelImageFilter(PZMath_matrix srcMatrix, out string logText)
        {
            // check image to minus 
            bool imageToMinusIsGrayLevel = eee.Sheffield.PZ.Imaging.Image.IsGraylevelImage(_imageToMinus);
            if (!imageToMinusIsGrayLevel)
                throw new Exception("TwoImageMinus::ApplyDoubleGraylevelImageFilter(), image to minus is not gray level image.");
            int width = _imageToMinus.Width;
            int height = _imageToMinus.Height;
            if (width != srcMatrix.ColumnCount || height != srcMatrix.RowCount)
                throw new Exception("TwoImageMinus::ApplyDoubleGraylevelImageFilter(), two image are not in the same demension.");

            ConvertBitmapMatrix converter = new ConvertBitmapMatrix();
            PZMath_matrix matrixToMinus = converter.ConvertGraylevelBitmapToMatrix(_imageToMinus);
            PZMath_matrix dstMatrix = new PZMath_matrix(height, width);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    dstMatrix[y, x] = srcMatrix[y, x] + matrixToMinus[y, x];
                }
            }
            dstMatrix.Normalize(0, 255);

            logText = "succeeds.";
            return dstMatrix;
        } // ApplyDoubleGraylevelImageFilter()

        protected override PZMath_matrix[] ApplyDoubleColourImageFilter(PZMath_matrix[] srcMatrixArray, out string logText)
        {
            // TODO: add this function

            throw new Exception("The method or operation is not implemented.");
        } // ApplyDoubleColourImageFilter()
        #endregion
    }
}
